import os
import sha
import random
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from api_key.model import ApiKey
from api_key import get_api_key_by_key

def generate_api_key(name, email):
    sh = sha.new()
    sh.update(str(name))
    sh.update(str(email))
    sh.update('%s' % random.random())
    sh.update('%s' % random.random())
    return sh.hexdigest()

def generate_secret_key(name, email):
    sh = sha.new()
    sh.update(str(name))
    sh.update(str(email))
    sh.update(generate_api_key(name, email))
    sh.update('%s' % random.random())
    sh.update('%s' % random.random())
    return sh.hexdigest()

class ListApiKey(webapp.RequestHandler):
    def get(self):
        apis = ApiKey.all()
        key = self.request.get('searched')
        apise = get_api_key_by_key(key)
        template_values = {
            'apis' : apis,
            'apise' : apise
        }

        path = os.path.join(os.path.dirname(__file__), 'list.html')
        self.response.out.write(template.render(path, template_values)) 
        
    def post(self):
        api = ApiKey.all()
        i = 0
        for a in api:
            if a.name.upper() == self.request.get('kq').decode('utf-8').upper():
                self.redirect('/api_key/list?searched=%s' % a.key(), True)
                i=i+1
                break
            if a.email == self.request.get('kq').decode('utf-8'):
                self.redirect('/api_key/list?searched=%s' % a.key(), True)
                i=i+1
                break
        if i == 0:
            self.response.out.write('Khong tim thay')

class AddApiKey(webapp.RequestHandler):
    def get(self):
        key = self.request.get('added')
        # TODO api = find api_key where __key__ = key
        api = get_api_key_by_key(key)

        template_values = {
            'api' : api
        }

        path = os.path.join(os.path.dirname(__file__), 'add.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        if self.request.get('api_name').decode('utf-8'):
            api = ApiKey()
            api.name = self.request.get('api_name').decode('utf-8')
            api.email = self.request.get('api_email').decode('utf-8')
            api.enabled = True if self.request.get('api_enabled') == 'on' else False
            api.api_key = generate_api_key(api.name, api.email)
            api.secret_key = generate_secret_key(api.name, api.email)
            api.put()

            self.redirect('/api_key/add?added=%s' % api.key(), True)
        else:
            self.redirect('/api_key/add' , True)
        
class DeleteApiKey(webapp.RequestHandler):    
    def get(self):
        key = self.request.get('key')
        api = get_api_key_by_key(key)
        if api:
            api.delete()
        
        self.redirect('/api_key/add?added=%s' % api.key(), True)

class DeleteApiKey2(webapp.RequestHandler):    
    def get(self):
        key = self.request.get('key')
        api = get_api_key_by_key(key)
        if api:
            api.delete()
        
        self.redirect('/api_key/list?deleted=%s' % api.key(), True)
        
class EditApiKey(webapp.RequestHandler):
    def get(self):
        key = self.request.get('key')
        api = get_api_key_by_key(key)
        template_values = {
            'api' : api
        }

        path = os.path.join(os.path.dirname(__file__), 'list_edit.html')
        self.response.out.write(template.render(path, template_values))
        
class UpdateEdit(webapp.RequestHandler):
    def post(self):
        key = self.request.get('key')
        apise = get_api_key_by_key(key)
        if apise:
            apise.name = self.request.get('api_name').decode('utf-8')
            apise.email = self.request.get('api_email').decode('utf-8')
            apise.enabled = True if self.request.get('api_enabled') == 'on' else False
            apise.save()
        apis = ApiKey.all()
        template_values = {
            'apis' : apis,
            'apise' : apise
        }

        path = os.path.join(os.path.dirname(__file__), 'list.html')
        self.response.out.write(template.render(path, template_values)) 

        
        